package hongcaosp.app.android.user.settings;

import android.content.Intent;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;

import com.tencent.liteav.demo.common.utils.FileUtils;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.io.File;
import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.login.RegisterActivity;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.AppVersionWrap;
import hongcaosp.app.android.modle.impl.OtherModelImpl;
import hongcaosp.app.android.user.settings.about.AboutActivity;
import hongcaosp.app.android.user.settings.info.EditUserInfoActivity;
import hongcaosp.app.android.user.settings.invite.InviteActivity;
import hongcaosp.app.android.user.settings.invite.WriteCodeActivity;
import hongcaosp.app.android.user.settings.privacy.PrivacyActivity;
import hongcaosp.app.android.user.settings.protocol.ProtocolActivity;
import hongcaosp.app.android.user.settings.safety.SafetyAndAccountActivity;
import hongcaosp.app.android.user.settings.share.DialogShareHome;
import hongcaosp.app.android.user.settings.treaty.TreatyActivity;
import hongcaosp.app.android.user.settings.update.DialogUpdate;
import hongcaosp.app.android.user.settings.wallet.WalletActivity;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.DialogUtil;
import xlj.lib.android.base.utils.StatusBarUtil;
import xlj.lib.android.base.utils.Util;

public class SettingsActivity extends BaseActivity {

    @Override
    protected void initData(Intent intent) {

    }

    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarDarkTheme(this,true);
        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        findViewById(R.id.layout_safety).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //账号与安全
                Intent intent = new Intent(SettingsActivity.this,SafetyAndAccountActivity.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.layout_editinfo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingsActivity.this,EditUserInfoActivity.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.layout_shareuserpage).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DialogShareHome(SettingsActivity.this,SettingsActivity.this).show();
            }
        });
        findViewById(R.id.layout_inviteforgift).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(UserToken.getDefault().getUser().getInvitationUser())) {
                    WriteCodeActivity.launcher(SettingsActivity.this);
                } else {
                    InviteActivity.launcher(SettingsActivity.this);
                }
            }
        });
        findViewById(R.id.layout_checkupdate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new OtherModelImpl().appVersion(Util.getVersionCode(SettingsActivity.this), new DataCallBack<AppVersionWrap>() {
                    @Override
                    public void onGetData(AppVersionWrap data) {
                        super.onGetData(data);
                        if (data == null || data.getAppVersion() == null || TextUtils.isEmpty(data.getAppVersion().getVersionUrl()) || !data.getAppVersion().getVersionUrl().endsWith(".apk")) {
                            ToastManager.getInstance().showToast("当前已是最新版本");
                            return;
                        }
                        new DialogUpdate(SettingsActivity.this,data.getAppVersion()).show();
                    }

                    @Override
                    public void onFail(int code, String errorMessage) {
                        super.onFail(code, errorMessage);
                        ToastManager.getInstance().showToast("当前已是最新版本");
                    }
                });
            }
        });
        findViewById(R.id.layout_wallet).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WalletActivity.launcher(SettingsActivity.this);
            }
        });
        findViewById(R.id.layout_userprotocol).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProtocolActivity.launcher(SettingsActivity.this);
            }
        });
        findViewById(R.id.layout_communityconvention).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TreatyActivity.launcher(SettingsActivity.this);
            }
        });
        findViewById(R.id.layout_privicypolicy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PrivacyActivity.launcher(SettingsActivity.this);
            }
        });
        findViewById(R.id.layout_about).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AboutActivity.launcher(SettingsActivity.this);
            }
        });
        findViewById(R.id.layout_cleancache).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AndPermission.with(SettingsActivity.this)
                        .runtime()
                        .permission(Permission.READ_EXTERNAL_STORAGE,Permission.WRITE_EXTERNAL_STORAGE)
                        .onGranted(new Action<List<String>>() {
                            @Override
                            public void onAction(List<String> data) {
                                //缓存目录，以后也许有其它的
                                //删除文件夹下面的文件，保留文件夹
                                final File rootFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/redgrass/txvodcache");
                                if (!rootFile.exists() || rootFile.listFiles().length == 0) {
                                    ToastManager.getInstance().showToast("暂无缓存");
                                    return;
                                }
                                final File[] files = rootFile.listFiles();

                                long size = 0;
                                for(File file : files) {
                                    size += FileUtils.getFileSize(file.getAbsolutePath());
                                }
                                size = size / 1024 / 1024;
                                DialogUtil.DialogBuilder
                                        .createBuilder(SettingsActivity.this)
                                        .setContent("确定清理缓存吗？("+size+"M)")
                                        .setSureText("确定")
                                        .setOnSureListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                for(File file : files) {
                                                    FileUtils.deleteFile(file.getAbsolutePath());
                                                }
                                                ToastManager.getInstance().showToast("清理成功");
                                            }
                                        })
                                        .setCancelText("取消")
                                        .build()
                                        .show();
                            }
                        })
                        .onDenied(new Action<List<String>>() {
                            @Override
                            public void onAction(List<String> data) {
                                ToastManager.getInstance().showToast("请给予我读写权限");
                            }
                        })
                        .start();
            }
        });
        findViewById(R.id.layout_exist).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.DialogBuilder
                        .createBuilder(SettingsActivity.this)
                        .setContent("是否退出当前账号？")
                        .setSureText("确定")
                        .setOnSureListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                UserToken.getDefault().clearToken();
                                Intent intent = new Intent(SettingsActivity.this,RegisterActivity.class);
                                startActivity(intent);
                                finish();

                            }
                        })
                        .setCancelText("取消")
                        .build()
                        .show();

            }
        });
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_settings;
    }
}
